﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Lu.Blog.Domain.Model.DocumentType;
using Lu.Blog.Domain.Utils;
using Lu.Blog.Models;
using umbraco.MacroEngines;
using Umbraco.Web.Mvc;
using Vega.USiteBuilder;

namespace Lu.Blog.Controllers
{
    public class HomePageSurfaceController : SurfaceController
    {
        //
        // GET: /HomePageSurface/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Slider()
        {
            var list = new List<SummaryItem>();
            var children =
                new DynamicNode(-1).Descendants().Where(o => o.NodeTypeAlias == "ContentPage");
            foreach (var dynamicChild in children)
            {
                var child = ContentHelper.GetByNodeId<ContentPage>(dynamicChild.Id);
                if (child.IsSlider)
                {
                    DynamicMedia image = XmlNodePickerHelper.GetImagesFromPicker(child.PreviewImage).First();
                    list.Add(new SummaryItem
                    {
                        SummaryBody = child.PreviewBody,
                        SummaryTitle = child.PreviewTitle,
                        SummaryImageUrl = (image != null ? image.NiceUrl : ""),
                        Page = child
                    });
                }
            }
            var viewModel = new SliderViewModel
            {
                Items = list
            };
            return PartialView("HomeSlider", viewModel);
        }
    }
}